/* Copyright 2012 Oliver Katz

    This file is part of Theta.

    Theta is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include "frame.h"

namespace parsing
{
	Frame::Frame()
	{
	}

	Frame::Frame(string f)
	{
		func = f;
	}

	void Frame::branch(Frame f)
	{
		branches.push_back(f);
	}

	string Frame::getFunction()
	{
		return func;
	}

	vector<Frame> Frame::getBranches()
	{
		return branches;
	}

	int Frame::calcSize()
	{
		int n = 1;
		for (vector<Frame>::iterator i = branches.begin(); i != branches.end(); i++)
			n += i->calcSize();
		return n;
	}

	string Frame::display()
	{
		stringstream ss;
		ss << func << "(";
		if (!branches.empty())
			ss << branches[0].display();
		for (int i = 1; i < (int)branches.size(); i++)
			ss << ", "  << branches[i].display();
		ss << ")";
		return ss.str();
	}

	bool Frame::containsFloats()
	{
		const string digits = "0123456789.";
		bool isNum = true;
		for (string::iterator i = func.begin(); i != func.end(); i++)
		{
			if (digits.find(*i) == string::npos)
			{
				isNum = false;
				break;
			}
		}

		if (isNum == true && func.find(".") != string::npos)
			return true;

		for (vector<Frame>::iterator i = branches.begin(); i != branches.end(); i++)
			if (i->containsFloats())
				return true;

		return false;
	}
}